package com.qj.yantaishan.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.common.core.domain.R;
import com.qj.yantaishan.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qj.yantaishan.common.annotation.Log;
import com.qj.yantaishan.common.enums.BusinessType;
import com.qj.yantaishan.domain.ProductGroup;
import com.qj.yantaishan.service.IProductGroupService;
import com.qj.yantaishan.common.core.controller.BaseController;
import com.qj.yantaishan.common.core.domain.AjaxResult;
import com.qj.yantaishan.common.utils.poi.ExcelUtil;
import com.qj.yantaishan.common.core.page.TableDataInfo;

/**
 * 商品分组Controller
 *
 * @author qihe
 * @date 2022-07-22
 */
@RestController
@RequestMapping("/yantaishan/ProductGroup")
public class ProductGroupController extends BaseController {
    private String prefix = "yantaishan/ProductGroup";

    @Autowired
    private IProductGroupService productGroupService;

    @GetMapping("list")
    public R list(@RequestParam(required = false, defaultValue = "1") Integer currentPage, @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<ProductGroup> productGroupPage = new Page<>(currentPage, pageSize);
        Page<ProductGroup> page = productGroupService.page(productGroupPage, new LambdaQueryWrapper<ProductGroup>().orderByAsc(ProductGroup::getSort));
        return R.ok(page);
    }

    @PostMapping("save")
    public R save(@RequestBody ProductGroup productGroup) {
        productGroup.setAddTime(DateUtils.getNowDate().getTime()/1000);
        productGroup.setIsDelete(0);
        productGroup.setStoreId(1);

        productGroupService.save(productGroup);
        return R.ok("添加成功");
    }

    @PutMapping("/update")
    public R update(@RequestBody ProductGroup productGroup) {
        productGroupService.updateById(productGroup);
        return R.ok("修改成功");
    }

    @DeleteMapping("/delete")
    public R delete(@RequestParam Integer groupId) {
        productGroupService.removeById(groupId);
        return R.ok("删除成功");
    }
}
